//
//  NSDate+Helper.m
//  Bluetooth
//
//  Created by hebidu on 15/7/18.
//  Copyright (c) 2015年 Boye. All rights reserved.
//

#import "NSDate+Helper.h"

@implementation NSDate (Helper)

/** 返回默认时间格式 yyyy-MM-dd HH:mm:ss */
+ (NSDateFormatter *)defaultDateFormatter {
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    formatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:8*3600];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh"];
    formatter.locale = locale;
    return formatter;
}


/** 返回当前时间的 字符串表达 */
+ (NSString *)getCurrentDateForStringFormatWithFormatter:(NSString *)formattString{
    
    NSDateFormatter *formatt = [self defaultDateFormatter];
    if (formattString) {
        formatt.dateFormat = formattString;
    }
    NSString *dateForstringFormat = [formatt stringFromDate:[NSDate date]];
    return dateForstringFormat;
}


/** 返回某个时间戳对应的时间的 字符串表达 */
+ (NSString *)getDateForStringFotmatWithTimeInterval:(NSTimeInterval)interval formatter:(NSString *)formatterString {
    
    NSDateFormatter *formatt = [self defaultDateFormatter];
    if (formatterString) {
        formatt.dateFormat = formatterString;
    }
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
    NSString *dateForstringFormat = [formatt stringFromDate:date];
    return dateForstringFormat;
}


/** 某个时间的时间戳(距离1970 经过的秒数) */
+ (NSTimeInterval)dateTimeIntervalWithDate:(NSDate *)date {
    
    NSTimeInterval interval = [date timeIntervalSince1970];
    return interval;
}


/** 当前时间的时间戳(距离1970 经过的秒数) */
+ (NSTimeInterval)currentDateTimeInterval {
    
    return [self dateTimeIntervalWithDate:[NSDate date]];
}


/** 获得某个时间的星期 周日-周六（对应int 1-7） */
+ (NSInteger)getDateWeekDayForIntFormatWithDate:(NSDate *)date {
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [calendar components:NSCalendarUnitWeekday fromDate:date];

    return comps.weekday;
}


/** 获得某个时间的星期 周日-周六（对应string） */
+ (NSString *)getDateWeekDayForStringFormatWithDate:(NSDate *)date {
    
    NSDictionary *weekDic = @{@"1":@"星期日",
                              @"2":@"星期一",
                              @"3":@"星期二",
                              @"4":@"星期三",
                              @"5":@"星期四",
                              @"6":@"星期五",
                              @"7":@"星期六",};
    
    NSString *weekday = [NSString stringWithFormat:@"%li", [NSDate getDateWeekDayForIntFormatWithDate:date]];
    return [weekDic objectForKey:weekday];
}



/** 根据生日的时间戳获得年龄 */
+ (NSString *)getAgeFromBirthdayTimeInterval:(NSTimeInterval)interval {
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *nowDate = [NSDate date];

    NSDate *birthDay = [NSDate dateWithTimeIntervalSince1970:interval];
    
    unsigned int unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    
    /** 获得从生日那天，到现在的详细时间信息 */
    NSDateComponents *components = [calendar components:unitFlags fromDate:birthDay toDate:nowDate options:0];
    NSLog(@"%@", components);
    
    /** 记录年月日信息 */
    NSString *year = components.year > 0 ? [NSString stringWithFormat:@"%li年", components.year] : @"";
    NSString *month = components.month > 0 ? [NSString stringWithFormat:@"%li月", components.month] : @"";;
    NSString *day = components.day > 0 ? [NSString stringWithFormat:@"%li天", components.day] : @"1天";;
    
    NSString *age = [NSString stringWithFormat:@"%@%@%@", year, month, day];

    return age;
}

/** 根据时间戳获得日期字符串*/
+(NSString *) getDateStringFromeNumber:(NSNumber *)number{
    NSTimeInterval time = [number doubleValue];
    NSDate * detaildate = [NSDate dateWithTimeIntervalSince1970:time];
    
//    NSString *formattString = [self defaultDateTimeFormatString];
    NSDateFormatter * formatt = [self defaultDateFormatter];
//    formatt.dateFormat = formattString;
    
    NSString * dateStr = [formatt  stringFromDate:detaildate];
    
    return dateStr;

}

//+(NSString *)defaultDateTimeFormatString{
//    return @"yyyy-MM-dd HH:mm";
//}

@end
